<?php 
	class Manage_branch extends CI_Controller{

		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_branch");
			$this->load->model("apanel/db_product");
			$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function index(){
			//== template
			redirect('manage_branch/cate_list');
		}
		
		public function cate_list($st = ""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			//list($item,$num,$page,$total) = $this->listall();
			//$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_branch/data_list",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 3] },
			{ "bSortable": false, "aTargets": [ 5 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
			//== template
			
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_branch/branch_cate_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/cate_list.html');
		}
		public function data_list(){
			$output = $this->db_branch->data_list();
			echo json_encode($output);
		}
		
		
		public function branch_cate_add($subid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("name_en","Category name","required");
				$this->form_validation->set_rules("name_zh_cn","Category name","trim");
				$this->form_validation->set_rules("name_th","Category name","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("description_en","Description","required");
				$this->form_validation->set_rules("description_zh_cn","Description","trim");
				$this->form_validation->set_rules("description_th","Description","trim");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name_en",set_value('name_en'));
					$this->cismarty->assign("name_zh_cn",set_value('name_zh_cn'));
					$this->cismarty->assign("name_th",set_value('name_th'));
					$this->cismarty->assign("customer",set_value('customer'));
					$this->cismarty->assign("description_en",set_value('description_en'));
					$this->cismarty->assign("description_zh_cn",set_value('description_zh_cn'));
					$this->cismarty->assign("description_th",set_value('description_th'));
						
					//set error
					$this->cismarty->assign("errorname_en",form_error('name_en'));
					$this->cismarty->assign("errordescription_en",form_error('description_en'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/branch/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 100;
							$config['height'] = 70;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}else{
						$picname="";
					}
					if(!$error){
						if($subid == ""){
							$subid = 0;
						}
						$data = array(
								"customer_pkid"=>trim($this->input->post('customer')),
								"parent_pkid"=>$subid,
								"name_en"=>trim($this->input->post('name_en')),
								"name_zh_cn"=>trim($this->input->post('name_zh_cn')),
								"name_th"=>trim($this->input->post('name_th')),
								"description_en"=>trim($this->input->post('description_en')),
								"description_zh_cn"=>trim($this->input->post('description_zh_cn')),
								"description_th"=>trim($this->input->post('description_th')),
								"img"=>$picname,
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('mobile_branch_category',$data);
						if($res) redirect("manage_branch/cate_list/success");
						else  redirect('manage_branch/cate_list/fail');
					}
				}
			}
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_cate_add/$subid"));
			$this->cismarty->assign("form_close",form_close());
			//$this->cismarty->assign("navi",$this->navi($subid));
			$this->cismarty->assign("url",base_url());
			$this->cismarty->display('apanel/branch/cate_add.html');
		}
		
		
		public function branch_cate_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("name_en","Category name","required");
				$this->form_validation->set_rules("name_zh_cn","Category name","trim");
				$this->form_validation->set_rules("name_th","Category name","trim");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("description_en","Description","required");
				$this->form_validation->set_rules("description_zh_cn","Description","trim");
				$this->form_validation->set_rules("description_th","Description","trim");
				
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name_en",set_value('name_en'));
					$this->cismarty->assign("name_zh_cn",set_value('name_zh_cn'));
					$this->cismarty->assign("name_th",set_value('name_th'));
					$this->cismarty->assign("customer",set_value('customer'));
					$this->cismarty->assign("description_en",set_value('description_en'));
					$this->cismarty->assign("description_zh_cn",set_value('description_zh_cn'));
					$this->cismarty->assign("description_th",set_value('description_th'));
						
					//set error
					$this->cismarty->assign("errorname_en",form_error('name_en'));
					$this->cismarty->assign("errordescription_en",form_error('description_en'));
					$this->cismarty->assign("errorcustomer",form_error('customer'));
				} else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/branch/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 100;
							$config['height'] = 70;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$this->db->where("pkid",$id);
							$uppic = array("img"=>$picname);
							$this->db->update("mobile_branch_category",$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>trim($this->input->post('customer')),
								"name_en"=>trim($this->input->post('name_en')),
								"name_zh_cn"=>trim($this->input->post('name_zh_cn')),
								"name_th"=>trim($this->input->post('name_th')),
								"description_en"=>trim($this->input->post('description_en')),
								"description_zh_cn"=>trim($this->input->post('description_zh_cn')),
								"description_th"=>trim($this->input->post('description_th')),
							
				
								"updated_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
					$this->db->where("pkid",$id);
					$res = $this->db->update('mobile_branch_category',$data);
					
					if($res) redirect('manage_branch/cate_list/success');
					else  redirect('manage_branch/cate_list/fail');
					}
				}
			}else {
 				$this->db->where('pkid',$id);
 				$res = $this->db->get('mobile_branch_category');
 				$row = $res->row();
				//echo "xxx". $row->customer_pkid;
 				$this->cismarty->assign("name_en",$row->name_en);
 				$this->cismarty->assign("name_zh_cn",$row->name_zh_cn);
 				$this->cismarty->assign("name_th",$row->name_th);
 				//$this->cismarty->assign("address",$row->address);
 				//$this->cismarty->assign("tel",$row->tel);
 				$this->cismarty->assign("description_en",$row->description_en);
 				$this->cismarty->assign("description_zh_cn",$row->description_zh_cn);
 				$this->cismarty->assign("description_th",$row->description_th);
 				$this->cismarty->assign("customer",$this->db_news->customer_list($row->customer_pkid));
				
				
 				
				}
			//== template
			$this->cismarty->assign("tiny",$this->template->tinymce());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_cate_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/cate_edit.html');
		}
		
		
		public function branch_cate_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_branch_category");
				if($res) redirect('manage_branch/cate_list/success');
			}
		}
		
		public function branch_cate_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_branch_category");
				}
			}
			redirect('manage_branch/cate_list/success');
		}
		
		
		
		
		public function branch_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			//list($item,$num,$page,$total) = $this->listall();
			//$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_branch/datalist",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 3] },
			{ "bSortable": false, "aTargets": [ 5 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
			//== template
			
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_branch/branch_delAll"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_list.html');
		}
		
		public function datalist(){
			$output = $this->db_branch->datalist();
			echo json_encode($output);
		}
		
		public function branch_add(){	     
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("category","Category","trim");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("address","Address","trim");
				$this->form_validation->set_rules("tel","Tel","trim");
			
			
				if($this->form_validation->run() == FALSE){
				
					$this->cismarty->assign("customer",set_value("customer"));
					$this->cismarty->assign("category",set_value("category"));
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("address",set_value('address'));
					$this->cismarty->assign("tel",set_value('tel'));
				
				
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
				}else {
					
						$data = array(
								"customer_pkid"=>$this->input->post('customer'),
								"category_pkid"=>$this->input->post('category'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_CN"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_cn"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"address"=>trim($this->input->post('address')),
								"tel"=>trim($this->input->post('tel')),
								"status"=>$this->input->post('status'),
								"created_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$res = $this->db->insert('mobile_branch',$data);

						if($res) redirect('manage_branch/branch_list/success');
						else  redirect('manage_branch/branch_list/fail');
				
					
					}
	
					
			}
	
				/*	$result = '<tr>
                  <td align="left">Map url</td>
                  <td class="hotelInfo"><label for="txtCategory"></label>
                  http://<input type="text" name="url1" id="txtCategory" value="" /><span class="errors">(www.xxxxx.com)</span></td>
                </tr>'; 
					$result .= ' <tr>
                   <td align="left" valign="top">Latitude</td>
                   <td class="hotelInfo"><input type="text" name="latitude"   value="" /></td>
                 </tr>'; 
					$result .= '<tr>
                   <td align="left" valign="top">Longitude</td>
                   <td class="hotelInfo"><input type="text" name="longitude"   value="" /></td>
                 </tr>'; */
				
			
			/*if($this->session->userdata('sess_customer_name') =='huawei' || $this->session->userdata('sess_access')== 'superadmin'){
				$this->cismarty->assign("result",$result);
			}else{
				$this->cismarty->assign("result","");
			}*/
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("customer",$this->db_product->show_customer());
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_add"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_add.html');
		}
		
		public function cate_no($id,$group=""){
			//echo "group=".$group;
			if($id == "") $id=0;
			$this->db->where('customer_pkid',$id);
			$this->db->where("parent_pkid",0);
			$this->db->order_by("pkid","asc");
			$res = $this->db->get('mobile_branch_category');
			$str ='<select name="category">';
			$str .= '<option value="">=select Category=</option>';
			foreach ($res->result_array() as $P){
				if($group == $P['pkid']){
					$check = 'selected="selected"';
				}else {
					$check = "";
				}
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
				$this->db->where("parent_pkid",$P['pkid']);
				$num = $this->db->count_all_results("mobile_branch_category");
				if($num > 0){
					$this->db->where("parent_pkid",$P['pkid']);
					$res2 = $this->db->get("mobile_branch_category")->result_array();
					foreach ($res2 as $sub){
						if($group == $sub['pkid']){
							$check = 'selected="selected"';
						}else {
							$check = "";
						}
						$str .='<option value="'.$sub['pkid'].'" '.$check.'>.....'.$sub['name_en'].'</option>';
						$this->db->where("parent_pkid",$sub['pkid']);
						$num2 = $this->db->count_all_results("mobile_branch_category");
						if($num2 > 0){
							$this->db->where("parent_pkid",$sub['pkid']);
							$res3 = $this->db->get("mobile_branch_category")->result_array();
							foreach ($res3 as $sub2){
								if($group == $sub2['pkid']){
									$check = 'selected="selected"';
								}else {
									$check = "";
								}
								$str .='<option value="'.$sub2['pkid'].'" '.$check.'>................'.$sub2['name_en'].'</option>';
							}
						}
					}
				}
			}
			$str .="</select>";
			echo $str;
		}
		
		public function branch_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("status","status","required");
				$this->form_validation->set_rules("title_en","name","required");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("category","Category","trim");
				$this->form_validation->set_rules("title_cn","name","trim");
				$this->form_validation->set_rules("title_th"," name","trim");
				$this->form_validation->set_rules("desc_en","Description","required");
				$this->form_validation->set_rules("desc_cn","Description","trim");
				$this->form_validation->set_rules("desc_th","Description","trim");
				$this->form_validation->set_rules("address","Address","trim");
				$this->form_validation->set_rules("tel","Tel","trim");
		
			
				
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("category",set_value("category"));
					$this->cismarty->assign("customer",set_value("customer"));
					$this->cismarty->assign("title_en",set_value('title_en'));
					$this->cismarty->assign("title_cn",set_value('title_cn'));
					$this->cismarty->assign("title_th",set_value('title_th'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
					$this->cismarty->assign("address",set_value('address'));
					$this->cismarty->assign("tel",set_value('tel'));
				
			
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errortitle_en",form_error('title_en'));
					$this->cismarty->assign("errorstatus",form_error('status'));
					$this->cismarty->assign("errordesc_en",form_error('desc_en'));
				} else {
					
						$data = array(
								"category_pkid"=>$this->input->post('category'),
								"name_en"=>trim($this->input->post('title_en')),
								"name_zh_CN"=>trim($this->input->post('title_cn')),
								"name_th"=>trim($this->input->post('title_th')),
								"description_en"=>addslashes(trim($this->input->post('desc_en'))),
								"description_zh_CN"=>addslashes(trim($this->input->post('desc_cn'))),
								"description_th"=>addslashes(trim($this->input->post('desc_th'))),
								"address"=>trim($this->input->post('address')),
								"tel"=>trim($this->input->post('tel')),
								"status"=>$this->input->post('status'),
								"updated_by"=>$this->session->userdata("sess_username"),
								"created_date"=>date("Y-m-d H:i:s")
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_branch',$data);
					
						if($res) redirect('manage_branch/branch_list/success');
						else  redirect('manage_branch/branch_list/fail');
				
				}
				
			}else {
 				$this->db->where('pkid',$id);
 				$res = $this->db->get('mobile_branch');
 				$row = $res->row();
				//echo "xxx". $row->customer_pkid;
 				$this->cismarty->assign("title_en",$row->name_en);
 				$this->cismarty->assign("title_cn",$row->name_zh_CN);
 				$this->cismarty->assign("title_th",$row->name_th);
 				$this->cismarty->assign("address",$row->address);
 				$this->cismarty->assign("tel",$row->tel);
 				$this->cismarty->assign("desc_en",$row->description_en);
 				$this->cismarty->assign("desc_cn",$row->description_zh_CN);
 				$this->cismarty->assign("desc_th",$row->description_th);
		

				$group 	= $row->customer_pkid;
				$cate		= $row->category_pkid;
				
			/*	$result = '<tr>
                  <td align="left">Map url</td>
                  <td class="hotelInfo"><label for="txtCategory"></label>
                  http://<input type="text" name="url1" id="txtCategory" value="'.$row->url.'" /><span class="errors">(www.xxxxx.com)</span></td>
                </tr>'; 
					$result .= ' <tr>
                   <td align="left" valign="top">Latitude</td>
                   <td class="hotelInfo"><input type="text" name="latitude"   value="'.$row->latitude.'" /></td>
                 </tr>'; 
					$result .= '<tr>
                   <td align="left" valign="top">Longitude</td>
                   <td class="hotelInfo"><input type="text" name="longitude"   value="'.$row->longitude.'" /></td>
                 </tr>'; */
				
				if($row->status == 1) $this->cismarty->assign("sa",'checked="checked"');
				else $this->cismarty->assign("sd",'checked="checked"');
 				
				
			}
		/*	if($this->session->userdata('sess_customer_name') =='huawei' || $this->session->userdata('sess_access')== 'superadmin'){
				$this->cismarty->assign("result",$result);
			}else{
				$this->cismarty->assign("result","");
			}*/
			//== template
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("customer",$this->db_product->show_customer($group));
			$this->cismarty->assign("cid","".$group."/$cate");
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,3));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_edit.html');
		}
		
		
		public function branch_view($id){
			$arr = array();
			$this->db->where("branch_pkid",$id);
			$this->db->order_by("pkid","desc");
			$res =$this->db->get("mobile_branch_img")->result_array();
			foreach ($res as $P){
				$tmp['pkid']		=$P['pkid'];
				$tmp['img']		='<img src="'.base_url().'media/branch/'.$P['img'].'">';
				$tmp['title']		=$P['name'];
				$tmp['cid']			=$id;
				$arr[] =$tmp;
			}
		
			$this->cismarty->assign("gallery",$arr);
			$this->cismarty->assign("total",$this->db_news->gallery_count($id));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("id",$id);
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_img_dell_all/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_view.html');
		}
		
		public function branch_img_add($cid){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("title","name","trim");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title",set_value('title'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/branch/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						//$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							//$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 640;
							$config['height'] = 390;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"branch_pkid"=>$cid,
								"name"=>trim($this->input->post('title')),
								"img"=>$picname,
						);
						$res = $this->db->insert('mobile_branch_img',$data);
						if($res){
							if($cid != ""){
								redirect("manage_branch/branch_view/$cid");
							}else{
								redirect('manage_branch/branch_list/success');
							}
						}
						else  redirect('manage_branch/branch_list/fail');
					}
				}
			}//end post
				
			$this->cismarty->assign("customer",$this->db_news->customer_list($cid));
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("cid",$cid);
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_img_add/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_img_add.html');
		}
		
		public function branch_img_edit($id,$cid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("title","Name","trim");
					
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("title",set_value('title'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/branch/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 640;
							$config['height'] = 390;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array("img"=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('mobile_branch_img',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"name"=>trim($this->input->post('title'))
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update('mobile_branch_img',$data);
						if($res){
							if($cid != ""){
								redirect("manage_branch/branch_view/$cid");
							}else{
								redirect('manage_branch/branch_list/success');
							}
						}
						else  redirect('manage_branch/branch_list/fail');
					}
				}
			}//end post
			else{
				$this->db->where("pkid",$id);
				$row = $this->db->get("mobile_branch_img")->row();
				$this->cismarty->assign("title",$row->name);
				if(trim($row->img) != ""){
					$this->cismarty->assign("img",'<img src="'.base_url().'media/branch/'.$row->img.'"><br>');
				}
			}
		
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(2,2));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("cid",$cid);
			$this->cismarty->assign("form_open",form_open_multipart("manage_branch/branch_img_edit/$id/$cid"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/branch/branch_img_edit.html');
		}
		
		public function branch_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_branch");
				if($res) redirect('manage_branch/branch_list/success');
			}
		}
		
		public function branch_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_branch");
				}
			}
			redirect('manage_branch/branch_list/success');
		}
		
		public function branch_img_del($id,$cid){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_branch_img");
				if($res) redirect("manage_branch/branch_view/$cid");
			}
		}
		
		public function branch_img_dell_all($id){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_branch_img");
				}
			}
			redirect("manage_branch/branch_view/$id");
		}
	}
?>